#include "buffer.h"
#include "qkeymetro.h"
#include "qkeymetro.h"
//#include <QTest>

Buffer::Buffer() :
        PoolableObject(PoolableObject::BUFFER),
        data_(new QKeyMetro::SoundData [QKeyMetro::StaticData::bufSize] () ),
        size_(QKeyMetro::StaticData::bufSize)


{
    QKeyMetro::StaticData::log(QString("BUF: created with size: %1").arg(QKeyMetro::StaticData::bufSize));
}

Buffer::~Buffer()
{
    delete [] data_;
}

 QKeyMetro::SoundData ** Buffer::getData(){
     //QKeyMetro::StaticData::log(QString("size of portOut buffer %1").arg(sizeof((* data_))));
    return & data_;
}


 void Buffer::changeSize(int newSize){ 
     m_.lock();
     delete [] data_;
     data_ = new QKeyMetro::SoundData[newSize]();
     size_  = newSize;
     m_.unlock();
     QKeyMetro::StaticData::log(QString("BUF: size changed to: %1").arg(newSize));
 }

 int Buffer::getSize(){
     //while(! m_.tryLock()) QTest::qWait(1);
     m_.lock();
     int s = size_ ;
     m_.unlock();
     return s;
 };


 void Buffer::clear(){
    if (size_ > 0) memset (data_ , 0, sizeof(QKeyMetro::SoundData) * size_ );
 }
